// [[ OnflowGenerator File UUID: 5c90c148108948cea1426ee14304ee8e  ]]
package com.itdct.onflow.system.monitor.service

import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import com.itdct.onflow.system.monitor.entity.SysMachineReportEntity
import com.itdct.onflow.system.monitor.mapper.SysMachineReportMapper
import com.itdct.onflow.system.monitor.qo.SysMachineReportEditQo
import com.itdct.onflow.system.monitor.qo.SysMachineReportSearchQo
import com.itdct.onflow.system.monitor.vo.SysMachineReportDetailVo
import com.itdct.onflow.system.monitor.vo.SysMachineReportListVo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * @author DCTANT
 * @date 2025-10-16 09:59:44
 * @version 1.0
 * @description 服务器监控数据上报服务
 */
@Service
class SysMachineReportService(
    val sysMachineReportMapper: SysMachineReportMapper
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysMachineReportEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysMachineReportEntity::class.java)
        val id = sysMachineReportMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysMachineReportEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysMachineReportMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysMachineReportMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysMachineReportEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysMachineReportMapper.selectByIdsEx(ids)
        sysMachineReportMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysMachineReportSearchQo, context: Context): RespVo<SysMachineReportDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysMachineReportMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("服务器监控数据上报", id))
        return success(vo.dict())
    }

    fun page(qo: SysMachineReportSearchQo, context: Context): RespPageVo<SysMachineReportListVo> {
        startPage(context)
        val pageData = sysMachineReportMapper.findByList(qo)
        return successPage(pageData.dict())
    }

    fun getLineChart(qo: SysMachineReportSearchQo, context: Context): RespVo<List<SysMachineReportListVo>> {
        qo.machineId ?: fail(ReturnCode.LACK_PARAM, "请传入服务器id")
        qo.showCount = onflowConfig.monitor.lineChartShowCount
        var list: List<SysMachineReportListVo> = sysMachineReportMapper.findLastLineChart(qo)
        list = list.reversed()
        for (sysMachineReportListVo in list) {
            sysMachineReportListVo.networkDownload?.also {
                sysMachineReportListVo.networkDownloadShow = BigDecimal(it).divide(BigDecimal(1024), 2, RoundingMode.HALF_UP).toString()
            }
            sysMachineReportListVo.networkUpload?.also {
                sysMachineReportListVo.networkUploadShow = BigDecimal(it).divide(BigDecimal(1024), 2, RoundingMode.HALF_UP).toString()
            }

            sysMachineReportListVo.diskWrite?.also {
                sysMachineReportListVo.diskWriteShow = BigDecimal(it).divide(BigDecimal(1024), 2, RoundingMode.HALF_UP).toString()
            }

            sysMachineReportListVo.diskRead?.also {
                sysMachineReportListVo.diskReadShow = BigDecimal(it).divide(BigDecimal(1024), 2, RoundingMode.HALF_UP).toString()
            }
        }
        return success(list)
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

// [[ OnflowGenerator Range (- selectFunctions -) End ]]
}